package com.cmas.platform.controller;

import java.util.Arrays;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import com.aidex.common.annotation.DataSource;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.TableDataInfo;
import com.aidex.common.core.page.PageDomain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.enums.DataSourceType;
import com.aidex.common.utils.poi.ExcelUtil;
import com.aidex.common.core.controller.BaseController;
import com.aidex.framework.cache.DictUtils;
import javax.validation.constraints.*;

import com.cmas.platform.domain.Section;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.cmas.platform.domain.PlatformConfig;
import com.cmas.platform.service.PlatformConfigService;


/**
 * 平台配置Controller
 * @author su
 * @email 1218819874@qq.com
 * @date 2023-03-15
 */
@DataSource(value = DataSourceType.SLAVE)
@RestController
@RequestMapping("/platform/platformConfig")
public class PlatformConfigController extends BaseController  {

    @Autowired
    private PlatformConfigService platformConfigService;

    /**
     * 查询平台配置列表
     */
    @PreAuthorize("@ss.hasPermi('platform:platformConfig:list')")
    @GetMapping("/list")
    public R<TableDataInfo> list(PlatformConfig platformConfig) {
        startPage();
        List<PlatformConfig> list = platformConfigService.selectPlatformConfigList(platformConfig);
        return R.data(getDataTable(list));
    }

    /**
     * 获取平台配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('platform:platformConfig:query')")
    @GetMapping(value = "/{id}")
    public R<PlatformConfig> detail(@PathVariable("id") String id) {
        return R.data(platformConfigService.getById(id));
    }

    /**
     * 新增平台配置
     */
    @PreAuthorize("@ss.hasPermi('platform:platformConfig:add')")
    @Log(title = "平台配置", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  PlatformConfig platformConfig) {
        return R.data(platformConfigService.save(platformConfig));
    }

    /**
     * 修改平台配置
     */
    @PreAuthorize("@ss.hasPermi('platform:platformConfig:edit')")
    @Log(title = "平台配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated PlatformConfig platformConfig) {
        return R.data(platformConfigService.updatePlatformConfig(platformConfig));
    }

    /**
     * 删除平台配置
     */
    @PreAuthorize("@ss.hasPermi('platform:platformConfig:remove')")
    @Log(title = "平台配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.data(platformConfigService.removeByIds(Arrays.asList(ids)));
    }


    /**
     * 校验版块编码的唯一性
     */
    @PreAuthorize("@ss.hasPermi('platform:section:query')")
    @Log(title = "版块管理", businessType = BusinessType.CHECK)
    @GetMapping(value = {"/checkCodeUnique/{code}/{id}", "/checkCodeUnique/{code}"})
    public R checkCodeUnique(@NotBlank(message = "版块编码不允许为空") @PathVariable("code") String code, @PathVariable(value = "id", required = false) String id) {
        PlatformConfig platformConfig = new PlatformConfig();
        platformConfig.setKey(code);
        platformConfig.setId(id);
        return R.data(platformConfigService.checkCodeUnique(platformConfig));
    }


    /**
     * 导出平台配置列表
     */
    @PreAuthorize("@ss.hasPermi('platform:platformConfig:export')")
    @Log(title = "平台配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(PlatformConfig platformConfig) {
        List<PlatformConfig> list = platformConfigService.selectPlatformConfigList(platformConfig);
        ExcelUtil<PlatformConfig> util = new ExcelUtil<PlatformConfig>(PlatformConfig.class);
        return util.exportExcel(list, "平台配置数据");
    }
	
    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

}
